/**
 * 手动创建测试期数的脚本
 */

const mysql = require('mysql2/promise');

const DB_CONFIG = {
    host: '43.143.253.188',
    port: 3306,
    user: 'nextjs',
    password: 'yEAiRE2jcxTyRBy5',
    database: 'nextjs',
    timezone: '+08:00'
};

async function createTestRound() {
    let connection;
    
    try {
        console.log('🔌 正在连接数据库...');
        connection = await mysql.createConnection(DB_CONFIG);
        console.log('✅ 数据库连接成功！\n');

        // 获取当前最大期数编号
        const [maxRoundResult] = await connection.execute(`
            SELECT MAX(roundNumber) as maxRound
            FROM casino_rounds 
            WHERE gameType = 1
        `);
        
        const nextRoundNumber = (maxRoundResult[0]?.maxRound || 0) + 1;
        console.log(`📊 下一个期数编号: ${nextRoundNumber}`);

        // 创建新的测试期数
        const startTime = new Date();
        const endTime = new Date(startTime.getTime() + 10 * 60 * 1000); // 10分钟后结束

        console.log('🎲 创建大小玩法测试期数...');
        const [diceResult] = await connection.execute(`
            INSERT INTO casino_rounds (gameType, roundNumber, startTime, endTime, status, totalGold, totalSilver, createdAt, updatedAt)
            VALUES (?, ?, ?, ?, 0, 0, 0, NOW(), NOW())
        `, [1, nextRoundNumber, startTime, endTime]);

        console.log('⚔️  创建赤壁玩法测试期数...');
        const [chibiResult] = await connection.execute(`
            INSERT INTO casino_rounds (gameType, roundNumber, startTime, endTime, status, totalGold, totalSilver, createdAt, updatedAt)
            VALUES (?, ?, ?, ?, 0, 0, 0, NOW(), NOW())
        `, [2, nextRoundNumber, startTime, endTime]);

        console.log('✅ 测试期数创建成功！');
        console.log(`   • 大小玩法期数ID: ${diceResult.insertId}, 期数编号: ${nextRoundNumber}`);
        console.log(`   • 赤壁玩法期数ID: ${chibiResult.insertId}, 期数编号: ${nextRoundNumber}`);
        console.log(`   • 开始时间: ${startTime.toLocaleString()}`);
        console.log(`   • 结束时间: ${endTime.toLocaleString()}`);
        console.log(`   • 状态: 下注中 (0)`);

        // 验证创建结果
        console.log('\n🔍 验证创建的期数...');
        const [newRounds] = await connection.execute(`
            SELECT id, gameType, roundNumber, startTime, endTime, status
            FROM casino_rounds 
            WHERE roundNumber = ?
            ORDER BY gameType
        `, [nextRoundNumber]);

        newRounds.forEach(round => {
            const gameTypeText = round.gameType === 1 ? '大小玩法' : '赤壁玩法';
            const statusText = round.status === 0 ? '下注中' : round.status === 1 ? '开奖中' : '已结算';
            console.log(`   • 期数ID: ${round.id}, 类型: ${gameTypeText}, 状态: ${statusText}`);
        });

        console.log('\n🎉 现在可以测试 act_id: 1332 接口了！');

    } catch (error) {
        console.error('❌ 创建测试期数失败:', error.message);
        console.error(error.stack);
    } finally {
        if (connection) {
            await connection.end();
            console.log('\n🔌 数据库连接已关闭');
        }
    }
}

// 运行创建脚本
if (require.main === module) {
    createTestRound().catch(console.error);
}

module.exports = { createTestRound }; 